/**
 * TabBar.m by Giraldo Rosales.
 * Visit www.liquidgear.net for documentation and updates.
 *
 * Copyright (c) 2009 Nitrogen Design, Inc. All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 **/
#if LG_USE_TABBAR

#import "LiquidGearAppDelegate.h"
#import "TabBar.h"
#import "Utils.h"
#import "WebController.h"

@implementation TabBar

@synthesize btn1, btn2, btn3, btn4, btn5, tabBar, tabButtons, viewController;
@synthesize btnLink1, btnLink2, btnLink3, btnLink4, btnLink5;

- (id)initWithController:(WebController *)controller {
	self.viewController = controller;
	
	CGRect winRect	= [[UIScreen mainScreen] applicationFrame];
	CGRect tabRect	= CGRectMake(0, winRect.size.height - 50, winRect.size.width, 50);
	tabBar					= [[UITabBar alloc] initWithFrame:tabRect];
	tabBar.hidden		= TRUE;
	tabBar.delegate = self;
	
	tabButtons			= [[NSMutableArray alloc] initWithCapacity:5];
	
	return self;
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
	NSString *link;
	
	switch ([item tag]) {
		case 1:
			link	= btnLink1;
			break;
		case 2:
			link	= btnLink2;
			break;
		case 3:
			link	= btnLink3;
			break;
		case 4:
			link	= btnLink4;
			break;
		case 5:
			link	= btnLink5;
			break;
	}
	
	[[[Utils alloc] autorelease] jsCall:viewController.webView type:@"" data:nil directCall:link];
}

- (void)setSelected:(int)btnNum {
	switch (btnNum) {
		case 1:
			tabBar.selectedItem = btn1;
			break;
		case 2:
			tabBar.selectedItem = btn2;
			break;
		case 3:
			tabBar.selectedItem = btn3;
			break;
		case 4:
			tabBar.selectedItem = btn4;
			break;
		case 5:
			tabBar.selectedItem = btn5;
			break;
		default:
			tabBar.selectedItem = nil;
			break;
	}
}

- (void)setBadge:(int)btnNum value:(NSString*)value {
	switch (btnNum) {
		case 1:
			btn1.badgeValue = value;
			break;
		case 2:
			btn2.badgeValue = value;
			break;
		case 3:
			btn3.badgeValue = value;
			break;
		case 4:
			btn4.badgeValue = value;
			break;
		case 5:
			btn5.badgeValue = value;
			break;
	}
}

- (UITabBarSystemItem)systemButton:(NSString*)type {
	UITabBarSystemItem icon;
	
	if([type isEqualToString:@"favorites"]) {
		icon	= UITabBarSystemItemFavorites;
	}
	else if([type isEqualToString:@"featured"]) {
		icon	= UITabBarSystemItemFeatured;
	}
	else if([type isEqualToString:@"topRated"]) {
		icon	= UITabBarSystemItemTopRated;
	}
	else if([type isEqualToString:@"recents"]) {
		icon	= UITabBarSystemItemRecents;
	}
	else if([type isEqualToString:@"contacts"]) {
		icon	= UITabBarSystemItemContacts;
	}
	else if([type isEqualToString:@"history"]) {
		icon	= UITabBarSystemItemHistory;
	}
	else if([type isEqualToString:@"bookmarks"]) {
		icon	= UITabBarSystemItemBookmarks;
	}
	else if([type isEqualToString:@"search"]) {
		icon	= UITabBarSystemItemSearch;
	}
	else if([type isEqualToString:@"downloads"]) {
		icon	= UITabBarSystemItemDownloads;
	}
	else if([type isEqualToString:@"mostRecent"]) {
		icon	= UITabBarSystemItemMostRecent;
	}
	else if([type isEqualToString:@"mostViewed"]) {
		icon	= UITabBarSystemItemMostViewed;
	} else {
		icon	= UITabBarSystemItemMore;
	}
	
	return icon;
}

- (void)addTab:(int)num title:(NSString *)title icon:(NSString *)icon callback:(NSString *)callback {
	NSDictionary *tabObj	= [NSDictionary dictionaryWithObjectsAndKeys:
													 [NSString stringWithFormat:@"%d", num], @"num",
													 title, @"title",
													 icon, @"icon",
													 callback, @"callback",
													 nil
	];
	
	[tabButtons insertObject:tabObj atIndex:num-1];
	
	if(num > 0 && num <= 5 && [tabButtons count] > 0) {
		[self resetTabs];
	}
}

- (void)removeTab:(int)num {
	if(num <= [tabButtons count] && num-1 > 0) {
		[tabButtons removeObjectAtIndex:num-1];
		[self resetTabs];
	}
}

- (void)resetTabs {
	NSMutableArray *tabItems = [[NSMutableArray alloc] initWithCapacity:5];
	NSDictionary *tabObj;
	int tabNum, g;
	NSString *tabTitle;
	NSString *tabIcon;
	NSString *tabLink;
	
	for(g=0; g<[tabButtons count]; g++) {
		tabObj		= [tabButtons objectAtIndex:g];
		tabNum		= [[tabObj objectForKey:@"num"] intValue];
		tabIcon		= [tabObj objectForKey:@"icon"];
		tabTitle	= [tabObj objectForKey:@"title"];
		tabLink		= [tabObj objectForKey:@"callback"];
		
		if(tabNum == 1) {
			btnLink1	= tabLink;
			
			if([tabIcon hasSuffix:@".png"]) {
				UIImage	*tabBarImage1 = [[UIImage imageNamed:tabIcon] retain];
				btn1 = [[UITabBarItem alloc] initWithTitle:tabTitle image:tabBarImage1 tag:1];
			} else {
				btn1 = [[UITabBarItem alloc] initWithTabBarSystemItem:[self systemButton:tabIcon] tag:1];
			}
			
			[tabItems addObject:btn1];
		}
		else if(tabNum == 2) {
			btnLink2	= tabLink;
			
			if([tabIcon hasSuffix:@".png"]) {
				UIImage	*tabBarImage2 = [[UIImage imageNamed:tabIcon] retain];
				btn2 = [[UITabBarItem alloc] initWithTitle:tabTitle image:tabBarImage2 tag:2];
			} else {
				btn2 = [[UITabBarItem alloc] initWithTabBarSystemItem:[self systemButton:tabIcon] tag:2];
			}
			
			[tabItems addObject:btn2];
		}
		else if(tabNum == 3) {
			btnLink3	= tabLink;
			
			if([tabIcon hasSuffix:@".png"]) {
				UIImage	*tabBarImage3 = [[UIImage imageNamed:tabIcon] retain];
				btn3 = [[UITabBarItem alloc] initWithTitle:tabTitle image:tabBarImage3 tag:3];
			} else {
				btn3 = [[UITabBarItem alloc] initWithTabBarSystemItem:[self systemButton:tabIcon] tag:3];
			}
			
			[tabItems addObject:btn3];
		}
		else if(tabNum == 4) {
			btnLink4	= tabLink;
			
			if([tabIcon hasSuffix:@".png"]) {
				UIImage	*tabBarImage4 = [[UIImage imageNamed:tabIcon] retain];
				btn4 = [[UITabBarItem alloc] initWithTitle:tabTitle image:tabBarImage4 tag:4];
			} else {
				btn4 = [[UITabBarItem alloc] initWithTabBarSystemItem:[self systemButton:tabIcon] tag:4];
			}
			
			[tabItems addObject:btn4];
		}
		else if(tabNum == 5) {
			btnLink5	= tabLink;
			
			if([tabIcon hasSuffix:@".png"]) {
				UIImage	*tabBarImage5 = [[UIImage imageNamed:tabIcon] retain];
				btn5 = [[UITabBarItem alloc] initWithTitle:tabTitle image:tabBarImage5 tag:5];
			} else {
				btn5 = [[UITabBarItem alloc] initWithTabBarSystemItem:[self systemButton:tabIcon] tag:5];
			}
			
			[tabItems addObject:btn5];
		}
	}
	
	if([tabItems count] > 0) {
		[tabBar setAlpha:1];
	} else {
		[tabBar setAlpha:0];	
	}
	
	[tabBar setItems:tabItems animated:YES];
}

- (void)hideBar {
	LiquidGearAppDelegate *LGdelegate = (LiquidGearAppDelegate*)[[UIApplication sharedApplication] delegate];
	
	if(LGdelegate.useTabBar) {
		tabBar.hidden		= TRUE;
		
		CGRect winRect	= viewController.webView.frame;
		CGRect appRect	= [[UIScreen mainScreen] applicationFrame];
		
		if(winRect.size.height < appRect.size.height) {
			winRect.size.height	+= 50;
		}
		
		LGdelegate.useTabBar		= FALSE;
		viewController.webView.frame = winRect;
	}
}

- (void)showBar {
	LiquidGearAppDelegate *LGdelegate = (LiquidGearAppDelegate*)[[UIApplication sharedApplication] delegate];
	
	if(!LGdelegate.useTabBar) {
		tabBar.hidden					= FALSE;
		LGdelegate.useTabBar	= TRUE;
		
		CGRect winRect	= viewController.webView.frame;
		CGRect tabRect	= tabBar.frame;
		
		winRect.size.height	-= 50;
		
		tabRect.origin.y	= winRect.size.height + winRect.origin.y;
		tabBar.frame			= tabRect;
		
		viewController.webView.frame	= winRect;
	}
}

- (void)dealloc {
	[tabBar release];
	[viewController release];
	[super dealloc];
}
@end
#endif